package vk;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * User: ekozobrodov
 */
public class VkAdapter {
    public static List<Video> getUserVideosList() {
        String videoXml = RequestHelper.doGet("video.getUserVideos", "uid=" + AuthComponent.getUid());
        return fillVideosFromXML(videoXml);
    }

    public static List<Video> searchVideos(String query) {
        String videoXml = RequestHelper.doGet("video.search", "q=" + query + "&hd=1&count=100");
        return fillVideosFromXML(videoXml);
    }

    private static List<Video> fillVideosFromXML(String xml) {
        List<Video> result = new ArrayList<Video>();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));

            Document doc = db.parse(is);
            NodeList nodes = doc.getElementsByTagName("video");
            Video newVideo;
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element)nodes.item(i);
                Element line;
                newVideo = new Video();

                NodeList node = element.getElementsByTagName("id");
                if (node != null) {
                    line = (Element) node.item(0);
                    newVideo.setId(Integer.parseInt(getStringValue(line)));
                }

                node = element.getElementsByTagName("title");
                if (node != null) {
                    line = (Element) node.item(0);
                    newVideo.setTitle(getStringValue(line));
                }

                node = element.getElementsByTagName("owner_id");
                if (node != null) {
                    line = (Element) node.item(0);
                    newVideo.setOwnerId(Integer.parseInt(getStringValue(line)));
                }

                node = element.getElementsByTagName("duration");
                if (node != null) {
                    line = (Element) node.item(0);
                    newVideo.setDuration(Integer.parseInt(getStringValue(line)));
                }

                node = element.getElementsByTagName("thumb");
                if (node != null) {
                    line = (Element) node.item(0);
                    newVideo.setThumb(getStringValue(line));
                }

                node = element.getElementsByTagName("player");
                if (node != null) {
                    line = (Element) node.item(0);
                    newVideo.setPlayer(getStringValue(line));
                }

                result.add(newVideo);
            }
            return result;
        } catch (Exception e) {
            System.err.println("Cannot parse xml: " + xml);
        }
        return null;
    }

    private static String getStringValue(Element e) {
        Node child = e.getFirstChild();
        return child.getNodeValue();
    }
}
